package exam.web.action;

import exam.business.service.UserGetService;
import exam.business.service.UserUpdateService;
import exam.common.key.KeyUser;
import exam.common.util.NullCheckUtil;
import exam.model.entity.TUserEntity;
import exam.web.action.base.UserActionBase;
/**
 * <p>EditUserAction</p>
 * 
 * @author Phong
 * @version 19/05/2012
 */
public class EditUserAction extends UserActionBase {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	public String execute() throws Exception {
		String port = null;
		TUserEntity tUserEntity = null;
		String btUpdate = request.getParameter("btSave");
		if (NullCheckUtil.isNUll(btUpdate)) {
			String userId = request.getParameter("userId");
			serviceInfo.put(KeyUser.USER_ID, userId);
			UserGetService  userGetService = new UserGetService();
			port = userGetService.executeService(serviceInfo);
			if(ERROR.equals(port)
					|| NONE.equals(port)) {
				return ERROR;
			}
			tUserEntity = serviceInfo.getObject(KeyUser.T_USER_ENTITY);
			copyFromTUserEntity(tUserEntity);
			return INPUT;
		} else {
			// luu thay doi
			tUserEntity = new TUserEntity();
			copyToTUserEntity(tUserEntity);
			serviceInfo.put(KeyUser.T_USER_ENTITY, tUserEntity);
			UserUpdateService userUpdateService = new UserUpdateService();
			port = userUpdateService.executeService(serviceInfo);
			if(FAIL.equals(port)) {
				addActionError("Cập nhật thông tin người dùng thất bại");
				return FAIL;
			}
			addActionMessage("Cập nhật thông tin người dùng thành công");
			return SUCCESS;
		}
	}
}
